package com.outstanding.his.inspect.facade;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.inspect.domain.Antibody;
import com.outstanding.his.inspect.domain.Bloodfat;
import com.outstanding.his.inspect.domain.Inspect;
import com.outstanding.his.inspect.domain.Kidney;
import com.outstanding.his.inspect.domain.Liver;
import com.outstanding.his.inspect.domain.Urine;
import com.outstanding.his.inspect.service.IInspectService;
import com.outstanding.his.patient.domain.Patient;
import com.outstanding.his.patient.service.IPatientService;

@Controller
@SessionAttributes("patient")
public class InspectController {

	private IInspectService inspectService;
	private IPatientService patientService;

	public void setInspectService(IInspectService inspectService) {
		this.inspectService = inspectService;
	}

	public void setPatientService(IPatientService patientService) {
		this.patientService = patientService;
	}

	@RequestMapping("/inspect/submit.do")
	public String inspectSubmit(@ModelAttribute("inspect") Inspect inspect,
			@ModelAttribute("antibody") Antibody antibody,
			@ModelAttribute("bloodfat") Bloodfat bloodfat,
			@ModelAttribute("kidney") Kidney kidney,
			@ModelAttribute("liver") Liver liver,
			@ModelAttribute("Urine") Urine urine,
			@ModelAttribute("patient") Patient patient) {
		if (patient.getInspect() != null) {
			inspect.setInspectId(patient.getInspect().getInspectId());
		}
		inspect.setAntibody(antibody);
		inspect.setBloodfat(bloodfat);
		inspect.setKidney(kidney);
		inspect.setLiver(liver);
		inspect.setUrine(urine);
		patient.setInspect(inspect);
		inspect = inspectService.submitInspect(inspect);
		patientService.updatePatient(patient);
		return "redirect:/inspect/view.do?patientId=" + patient.getPatientId();
	}

	@RequestMapping("/inspect/view.do")
	public String inspectView(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/inspect/view";
	}

	@RequestMapping(value = "/inspect/edit.do", method = RequestMethod.GET)
	public String inspectEdit(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/inspect/edit";
	}

}
